package com.zryc.shop.elasticsearch;

import com.zryc.shop.pms.entity.Product;
import com.zryc.shop.pms.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Iterator;
import java.util.List;

@Configuration
public class ProductConfiguration {

    @Autowired
    private ProductRepository productRepository;

    @Autowired
    private IProductService productService;

    @Bean
    public void init() {
        productRepository.deleteAll();
        List<Product> list = productService.list();
        Iterable<Product> products = new Iterable<Product>() {
            @Override
            public Iterator<Product> iterator() {
                return list.iterator();
            }
        };
        productRepository.saveAll(products);
    }
}
